package com.ruoyi.core.lock.actuator;

import com.ruoyi.common.exception.ParamDeficiencyException;
import com.ruoyi.core.lock.IExpressionActuator;
import com.ruoyi.core.lock.LockModel;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @Author xiaom
 * @Date 2020/3/16 11:16
 * @Version 1.0.0
 * @Description <${}格式 获取非对象形式参数>
 **/
@Slf4j
public class ParamActuator implements IExpressionActuator {
    @Override
    public Collection<String> doExecute(LockModel lockModel) {
        Collection<String> expressions = lockModel.getExpression();
        if(expressions.isEmpty()){
            log.error("ParamActuator execute failed. Expression collection is empty.");
            throw new ParamDeficiencyException("Server Error..");
        }
        Object[] args = lockModel.getOriginalModels();
        List<String> keys = new ArrayList<>();
        for (int i = 0; i < args.length; i++) {
            if(expressions.contains((i + 1) + "")){
                if(args[i] == null){
                    continue;
                }
                keys.add(args[i].toString());
            }
        }
        return keys;
    }
}
